package com.ysdcrm.crm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.ysdcrm.crm.mapper.ICustomerMapper;
import com.ysdcrm.crm.mapper.IRolesMapper;
import com.ysdcrm.crm.model.Customers;
import com.ysdcrm.crm.model.Roles;
import com.ysdcrm.crm.model.Users;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

@RestController
@ApiOperation(value = "客户管理")
@RequestMapping("customer")
public class CustomersController {
    @Resource
    private ICustomerMapper mapper;
    @Resource
    private IRolesMapper rolesMapper;

    @ApiOperation(value = "我的客户",notes = "不同的用户看到的客户列表是不一样的")
    @GetMapping
    public List<Customers> findMyCustomers(HttpSession session){
        List<Customers> list = new ArrayList<>();
        
        //从session中获取登录用户信息
        Users user = (Users) session.getAttribute("user");
        //根据uid查询用户的角色
        Roles role = rolesMapper.findByUid(user.getUid());
        if("网络咨询师".equals(role.getName())){
            list = mapper.findByRecorder(user.getUid());
        }else if("咨询师".equals(role.getName())){
            list = mapper.findByConsultant(user.getUid());
        }else if("咨询经理".equals(role.getName())){
            list = mapper.selectList(null);
        }
        return list;//未完待续

    }

    @ApiOperation(value = "添加客户")
    @PostMapping
    public int add(Customers c){
        return mapper.insert(c);
    }

    @ApiOperation(value = "修改客户信息")
    @PutMapping
    public int edit(Customers c){
        return mapper.updateById(c);
    }

    @ApiOperation(value = "删除客户",notes = "逻辑删除")
    @DeleteMapping
    public int del(int cid){
        return mapper.del(cid);
    }

    @ApiOperation(value = "批量修改客户所属咨询师")
    @PostMapping("change")
    public int changeConsultant(int oldConsultant,int newConsultant){
        return mapper.changeConsultant(oldConsultant, newConsultant);
    }


}
